package top.laoshuzi.common.mvp.base;

import android.os.Bundle;
import android.support.annotation.Nullable;

import me.yokeyword.fragmentation.SupportFragment;
import top.laoshuzi.common.mvp.factory.PresenterFactory;
import top.laoshuzi.common.mvp.factory.ReflectionPresenterFactory;
import top.laoshuzi.common.mvp.presenter.PresenterLifecycleDelegate;
import top.laoshuzi.common.mvp.view.View;

public class BaseMVPFragment extends SupportFragment implements View {

    private PresenterLifecycleDelegate presenterDelegate = new PresenterLifecycleDelegate(ReflectionPresenterFactory.fromViewClass(this));

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenterDelegate.create(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenterDelegate.destroy(!requireActivity().isChangingConfigurations());
    }

    @Override
    public PresenterFactory getPresenterFactory() {
        return presenterDelegate.getPresenterFactory();
    }

    @Override
    public void setPresenterFactory(PresenterFactory presenterFactory) {
        presenterDelegate.setPresenterFactory(presenterFactory);
    }
}
